import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:magic_car/UI/bottom_Menu_page.dart';
import 'package:magic_car/UI/userbottom_Menu_page.dart';
import 'package:magic_car/baoxiu/brandselect.dart';
import 'package:magic_car/baoxiu/guarantee.dart';
import 'package:magic_car/baoxiu/insurance.dart';
import 'package:magic_car/inspect/inspector.dart';
import 'package:magic_car/jiance/b_guratee.dart';
import 'package:magic_car/jiance/b_mycar.dart';
import 'package:magic_car/jiance/b_order.dart';
import 'package:magic_car/jiance/jiance_detail.dart';
import 'package:magic_car/jiance/jiance_rec.dart';
import 'package:magic_car/jiance/jianceorder.dart';
import 'package:magic_car/jiance/painter.dart';
import 'package:magic_car/jiance/uorder_page.dart';
import 'package:magic_car/site/addsite.dart';
import 'package:magic_car/user/acount.dart';
import 'package:magic_car/user/apply.dart';
import 'package:magic_car/user/applytest.dart';
import 'package:magic_car/user/bluetest.dart';
import 'package:magic_car/user/charger.dart';
import 'package:magic_car/user/code.dart';
import 'package:magic_car/user/contact.dart';
import 'package:magic_car/user/distube.dart';
import 'package:magic_car/user/draw.dart';
import 'package:magic_car/user/drawrec.dart';
import 'package:magic_car/user/exam.dart';
import 'package:magic_car/user/examrec.dart';
import 'package:magic_car/user/forgetpass.dart';
import 'package:magic_car/user/login.dart';
import 'package:magic_car/user/moneydetail.dart';
import 'package:magic_car/user/regist.dart';
import 'package:magic_car/user/set.dart';
import 'package:magic_car/user/start.dart';
import 'package:magic_car/util/config.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; 
//import 'package:amap_base/amap_base.dart';
//import 'package:flutter_localizations/flutter_localizations.dart'; 
//import 'package:amap_location/amap_location.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:fluwx/fluwx.dart';
// void main() => runApp(MyApp());
void main() async{

   SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(statusBarColor: Colors.transparent)
  );
  //  SharedPreferences.setMockInitialValues({});
  
  runApp(new MyApp());
}
// SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor:Colors.transparent);
// SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
class MyApp extends StatelessWidget {
  

 
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
     EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 1000)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.light
      ..indicatorSize = 45.0
      ..radius = 10.0
      ..fontSize = 13
      ..backgroundColor = Colors.green
      ..indicatorColor = Colors.yellow
      ..textColor = Colors.yellow
      ..maskColor = Colors.blue.withOpacity(0.5);
    return MaterialApp(
      
      title: '车魔估合作商',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue
      ),
      home: MyHomePage(),
       builder: (context, child) {
                  return FlutterEasyLoading(child: child);

                  /// 保证文字大小不受手机系统设置影响 https://www.kikt.top/posts/flutter/layout/dynamic-text/
                  // return MediaQuery(
                  //   data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                  //   child: child,
                  // );
                },
       localizationsDelegates: [
        //此处
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        //此处
        const Locale('zh', 'CH'),
        const Locale('en', 'US'),
      ],
      routes: {
        'index':(BuildContext context)=>new MyApp(),
        'uindex':(BuildContext context)=>new IOrderPage(),
        "jiance_rec":(BuildContext context)=>new OrderRecPage(),
        // "jianceorder_detail":(BuildContext context)=>new JIanceDetailPage(),
        "contact":(BuildContext context)=>new ContactPage(),
        "regist":(BuildContext context)=>new RegistPage(),
        "forgetpass":(BuildContext context)=>new ForgetPassPage('1'),
        "set":(BuildContext context)=>new SetPage(),
        "wallet":(BuildContext context)=>new AcountPage(),
        "moneydetail":(BuildContext context)=>new MoneyRecPage(),
        "testrec":(BuildContext context)=>new ExamRecPage(),
        "draw":(BuildContext context)=>new DrawPage('1'),
        "drawrec":(BuildContext context)=>new DrawrecPage(),
        "csapply":(BuildContext context)=>new ApplyPage(),
        "code":(BuildContext context)=>new CodePage(),
        "exam":(BuildContext context)=>new ExamPage(),
        "applytest":(BuildContext context)=>new ApplyTestPage(),
        "upAsk":(BuildContext context)=>new UpAskPage(),
        'painter':(BuildContext context)=>new DemoApp(),
        'b_order':(BuildContext context)=>new BOrderRecPage(),
        'b_Gurateeorder':(BuildContext context)=>new BGurateOrderRecPage(),
        'mycar':(BuildContext context)=>new MycarPage('1'),
        'addsite':(BuildContext context)=>new AddSitePage(),
        'inspetor':(BuildContext context)=>new InspectorPage(),
        'applybussiness':(BuildContext context)=>new ApplyPage(),
        'charger':(BuildContext context)=>new ChargerPage(),
        'blue':(BuildContext context)=>new BluePage(),
        // 'insurance_company':(BuildContext context)=>new Insurance_Page(),
       // 'brand':(BuildContext context)=>new BrandSelectRoute(),
        
        //"guarantee":(BuildContext context)=>new GuaranteePage(),
        
        //"login":(BuildContext context)=>new LoginPage(),
      },
      // localizationsDelegates: [
      //   locale: Locale('zh'),
      //   GlobalMaterialLocalizations.delegate,
      //   GlobalWidgetsLocalizations.delegate,
      // ],
      // supportedLocales: [
      //   //此处
      //   locale: Locale('zh'),
      //   const Locale('zh', 'CH'),
      //   const Locale('en', 'US'),
      // ],

    );
  }
}
 checkLogin() async{ 
   return StartPage();
//   SharedPreferences pref = await SharedPreferences.getInstance();
//  var islogin = pref.getString('islogin');
//  if(islogin=="1"){
//    return BotomeMenumPage();
//  }else
// return StartPage();
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String debugLable = 'Unknown';   /*错误信息*/
final JPush jpush = new JPush();
  int _counter = 0;
var loginState;
  var _role;

   get() async {
    var islogin;

    final prefs = await SharedPreferences.getInstance();
    islogin = prefs.getString("islogin");
    var role = prefs.getString("role");

    setState(() {
      loginState = islogin;
      _role = role;
    });
  }

  void checkLoginState() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var states = await prefs.getString('login');
    print('登录状态' + states);

    setState(() {
      loginState = states;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _initFluwx();
    get();
    jpush.getRegistrationID().then((rid) async{
      print('---->rid:${rid}');
      SharedPreferences pref = await SharedPreferences.getInstance();
      pref.setString('registeid', rid);
    });
    jpush.setup(appKey: 'e44484d672dc336c98d2356b' ,channel: 'developer-default',production: false,debug: true);
    /// iOS千万记得加上这句！！！！！！要不没用
    jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));
    jpush.addEventHandler(
      onReceiveNotification: (Map<String, dynamic> message) async {
        print('收到消息');
        print(message);
      },
      onOpenNotification: (Map<String, dynamic> message) async {
        /// 点击通知栏消息，在此时通常可以做一些页面跳转等
        print('点击通知');
        print(message);
      },
    ); 
  }
  _initFluwx() async {
    await registerWxApi(
        appId: Config.wx_appid,
        doOnAndroid: true,
        doOnIOS: true,
        universalLink: "https://app.chemogu1688.com/");
    var result = await isWeChatInstalled;
    print("is installed $result");
  }

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    
 if (loginState == "1") {
      if (_role == "1")
        return new UBotomeMenumPage();
      else
        return new BotomeMenumPage();
    } else
      return new StartPage();
  }
   
}
